package Program.RegisterUI;

import Program.LoginUI.LoginUI;
import Program.SqlTool.UserDAO;
import Program.Tool.Popup;
import javax.swing.*;
import java.awt.event.*;

public class UserRegisterListener implements ActionListener {
    //用户注册界面
    private JFrame UserRegisterUI;
    //顶端容器
    private JPanel northPanel;
    //账号输入框
    private JTextField userNameField;
    //密码输入框
    private JPasswordField passwordField;
    //姓名输入框
    private JTextField nameField;
    //性别复选框
    private JComboBox<String> sexBox;
    //用户类型复选框
    private JComboBox<String> usertypeBox;
    //手机号
    private JTextField phoneField;
    //门牌信息输入框
    private JTextArea placeJTextArea;
    //最小化按钮
    private JButton minimizeButton;
    //关闭按钮
    private JButton closeButton;
    //注册按钮
    private JButton registerButton;
    //返回按钮
    private JButton backButton;
    //记录鼠标x轴上的位置
    private int mouseAtX = 0;
    //记录鼠标y轴上的位置
    private int mouseAtY = 0;

    public UserRegisterListener() {
        super();
    }

    public UserRegisterListener(JFrame userRegisterUI, JPanel northPanel, JTextField userNameField,
                                JPasswordField passwordField, JTextField nameField, JComboBox<String> sexBox,
                                JComboBox<String> usertypeBox, JTextField phoneField, JTextArea placeJTextArea,
                                JButton minimizeButton, JButton closeButton, JButton registerButton, JButton backButton) {
        UserRegisterUI = userRegisterUI;
        this.northPanel = northPanel;
        this.userNameField = userNameField;
        this.passwordField = passwordField;
        this.nameField = nameField;
        this.sexBox = sexBox;
        this.usertypeBox = usertypeBox;
        this.phoneField = phoneField;
        this.placeJTextArea = placeJTextArea;
        this.minimizeButton = minimizeButton;
        this.closeButton = closeButton;
        this.registerButton = registerButton;
        this.backButton = backButton;
        this.canDraged(this.northPanel);
    }

    //设置窗口移动
    public void canDraged(JPanel p){
        //创建接收鼠标事件的监听器
        MouseAdapter mouseAdapter=new MouseAdapter() {
            //当鼠标按下时获取窗口的坐标位置
            public void mousePressed(MouseEvent e) {
                mouseAtX = e.getPoint().x;
                mouseAtY = e.getPoint().y;
            }
        };
        MouseMotionAdapter motionAdapter=new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                //设置获取拖拽后的位置并窗口的位置
                UserRegisterUI.setLocation((e.getXOnScreen()-mouseAtX),(e.getYOnScreen()-mouseAtY));
            }
        };
        //给顶部容器添加监听事件
        p.addMouseListener(mouseAdapter);
        p.addMouseMotionListener(motionAdapter);
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==registerButton){
            registerCheck();
        }else if(e.getSource()== closeButton){
            //触发关闭事件
            System.exit(0);
        }else if(e.getSource()==minimizeButton) {
            //触发最小化事件
            UserRegisterUI.setExtendedState(JFrame.ICONIFIED);
        }else if (e.getSource()==backButton){
            //触发返回事件
            SwingUtilities.invokeLater(() -> new LoginUI());
            UserRegisterUI.dispose();
        }
    }

    //注册检查
    private void registerCheck(){
        //从用户名框里读取用户名
        String inputUserName = userNameField.getText();
        //从密码输入框里读取密码
        char[] tempArray = passwordField.getPassword();
        String inputPassword = new String(tempArray);
        //从姓名输入框里读取姓名
        String inputName = nameField.getText();
        //从性别下拉框里读取性别
        String inputSex = sexBox.getSelectedItem().toString();
        //从用户类型下拉框里读取用户类型
        String inputUsertype = usertypeBox.getSelectedItem().toString();
        //从手机号输入框里读取手机号
        String inputPhone = phoneField.getText();
        //从门牌信息输入框里读取门牌信息
        String inputPlace = placeJTextArea.getText();
        //提示信息d
        String info = "";
        if(inputName.length()*inputPassword.length()*inputName.length()*inputPhone.length()*inputPlace.length() == 0){
            // 判断所有输入框是否为空
            info = "<html>注册失败,请将所有信息完善</html>";
            Popup.showJDialog(info);
        }else if(!(inputUserName.length()>=4 && inputUserName.length() <= 17)) {
            //判断用户名格式是否正确，最小长度4，最大长度17
            info = "<html>用户名要求：最小长度为4，最大长度为17</html>";
            Popup.showJDialog(info);
            //清空输入框
            userNameField.setText("");
        }else if (!(inputPassword.length() >=4 && inputPassword.length() <= 17)){
            //判断密码格式是否正确，最小长度4，最大长度17
            info = "<html>密码要求：最小长度为4，最大长度为17</html>";
            Popup.showJDialog(info);
            //清空输入框
            passwordField.setText("");
        } else if (!nameCheck(inputName)) {
            //判断姓名格式是否正确
            info = "<html>请填写正确的中文姓名</html>";
            Popup.showJDialog(info);
            //清空输入框
            nameField.setText("");
        } else if (!phoneCheck(inputPhone)) {
            //判断手机号格式是否正确，长度为11
            info = "<html>请填写正确的手机号</html>";
            Popup.showJDialog(info);
            //清空输入框
            phoneField.setText("");
        } else if (!PFCheck(inputPlace)) {
            //判断门牌信息格式是否正确
            info = "<html>门牌信息格式：xx小区xx栋xx单元xx号</html>";
            Popup.showJDialog(info);
            //清空输入框
            placeJTextArea.setText("");
        } else {
            //进行注册操作，数据写入数据库
            boolean flag = UserDAO.addNewUser(inputUserName, inputPassword, inputName, inputSex, inputUsertype, inputPhone, inputPlace);
            if (flag) {
                info = "<html>注册成功</html>";
                Popup.showJDialog(info);
                SwingUtilities.invokeLater(() -> new LoginUI());
                UserRegisterUI.dispose();
            }else {
                info = "<html>注册失败</html>";
                Popup.showJDialog(info);
                //清空输入框
                userNameField.setText("");
                //清空输入框
                passwordField.setText("");
                //清空输入框
                nameField.setText("");
                //清空输入框
                phoneField.setText("");
                //清空输入框
                placeJTextArea.setText("");
            }
        }

    }

    //姓名格式检查
    private boolean nameCheck(String inputName){
        String regex ="^(?:[\\u4e00-\\u9fa5·]{2,16})$";
        boolean flag = inputName.matches(regex);
        return flag;
    }

    //手机号格式检查
    private boolean phoneCheck(String inputPhone){
       String regex = "^1(?:(?:3[\\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[1|8|9]))\\d{8}$";
       boolean flag = inputPhone.matches(regex);
       return flag;
    }


    //门牌信息格式检查
    private boolean PFCheck(String inputPlace){
        //xx小区xx栋xx单元xx号
        String regex= "^[\\u4E00-\\u9FA5a-zA-Z]{1,5}小区\\d{1,2}栋\\d{1,2}单元\\d{1,4}号$";
        boolean flag = inputPlace.matches(regex);
        return flag;
    }
}
